import pygame
import Vertex
import math
import utils
import copy
import pdb

class Me(pygame.sprite.Sprite):
	def __init__(self, vertex):
		pygame.sprite.Sprite.__init__(self)
		self.image, self.rect = utils.load_image("me.png",(255,255,255))
		self.orig = copy.copy(self.image)
		self.angle = 0
		self.vertex = vertex
		self.rect.center = vertex.get_pos()
		self.adjacents = vertex.get_adjacents()
		self.index = 0
		self.next = self.adjacents[self.index]
		
		x, y = self.vertex.get_pos()
		z, t = self.next.get_pos()
		if z - x == 0:
			self.angle = 90.0
		else:
			self.angle = math.degrees(math.atan(math.fabs(t - y) / math.fabs(z - x)))
		#this is to look down		
		if t - y > 0:
			self.angle = 360 - self.angle
		#this is to look back
		if z - x < 0:
			self.angle = 180 - self.angle

		self.image = pygame.transform.rotate(self.orig, self.angle)
		self.rect = self.image.get_rect()
		self.rect.center = self.vertex.get_pos()
		self._sort()

	def go_to(self, x, y):
		for v in self.adjacents:
			if x-10 < v.get_x() < x+10 and y-10 < v.get_y() < y+10:
				self.set_vertex(v)

	def set_vertex(self, vertex):
		self.vertex = vertex
		self.rect.center = self.vertex.get_pos()
		self.adjacents = self.vertex.get_adjacents()
		self.index = 0
		self.next = self.adjacents[self.index]
		x, y = self.vertex.get_pos()
		z, t = self.next.get_pos()
		if z - x == 0:
			self.angle = 90.0
		else:
			self.angle = math.degrees(math.atan(math.fabs(t - y) / math.fabs(z - x)))
		#this is to look down		
		if t - y > 0:
			self.angle = 360 - self.angle
		#this is to look back
		if z - x < 0:
			self.angle = 180 - self.angle
		
		self.image = pygame.transform.rotate(self.orig, self.angle)
		self._sort()

	def rotate_left(self):
		self.index += 1
		self.index %= len(self.adjacents)
		self.next = self.adjacents[self.index]
		x, y = self.vertex.get_pos()
		z, t = self.next.get_pos()
		if z - x == 0:
			self.angle = 90.0
		else:
			self.angle = math.degrees(math.atan(math.fabs(t - y) / math.fabs(z - x)))
		#this is to look down		
		if t - y > 0:
			self.angle = 360 - self.angle
		#this is to look back
		if z - x < 0:
			self.angle = 180 - self.angle

		self.image = pygame.transform.rotate(self.orig, self.angle)
		self.rect = self.image.get_rect()
		self.rect.center = self.vertex.get_pos()
	
	def rotate_right(self):
		self.index -= 1
		self.index %= len(self.adjacents)
		self.next = self.adjacents[self.index]
		x, y = self.vertex.get_pos()
		z, t = self.next.get_pos()
		if z - x == 0:
			self.angle = 90.0
		else:
			self.angle = math.degrees(math.atan(math.fabs(t - y) / math.fabs(z - x)))
		#this is to look down		
		if t - y > 0:
			self.angle = 360 - self.angle
		#this is to look back
		if z - x < 0:
			self.angle = 180 - self.angle
		
		self.image = pygame.transform.rotate(self.orig, self.angle)
		self.rect = self.image.get_rect()
		self.rect.center = self.vertex.get_pos()
	
	def go_next(self):
		self.vertex = self.next
		self.rect.center = self.vertex.get_pos()
		self.adjacents = self.vertex.get_adjacents()
		self.index = 0
		self.next = self.adjacents[self.index]
		x, y = self.vertex.get_pos()
		z, t = self.next.get_pos()
		if z - x == 0:
			self.angle = 90.0
		else:
			self.angle = math.degrees(math.atan(math.fabs(t - y) / math.fabs(z - x)))
		#this is to look down		
		if t - y > 0:
			self.angle = 360 - self.angle
		#this is to look back
		if z - x < 0:
			self.angle = 180 - self.angle
		
		self.image = pygame.transform.rotate(self.orig, self.angle)
		self._sort()


	def _sort(self):
		"""
			this function sort the adjacent vertices vector by angle, to 
			rotate with keys left and right
		"""
		sorted = [[self.next, self.angle]]
		aux = self.next
		self.rotate_left()
		while self.next != aux:
			sorted.append([self.next,self.angle])
			i = len(sorted)-2
			angle = self.angle
			if angle < 0:
				angle = 360 + angle
			while (angle < sorted[i][1]) and (i >= 0):
				sorted[i+1] = sorted[i]
				sorted[i] = [self.next,angle]
				i-=1

			self.rotate_left()
		
		for i,s in enumerate(sorted):
			self.adjacents[i] = s[0]
